package org.example.java.swing;

import javax.swing.*;
import java.awt.event.*;

import static org.example.util.Print.print;

class MyWindowEventHandle implements WindowListener {

  @Override
  public void windowOpened(WindowEvent e) {
    print("窗口打开");
  }

  @Override
  public void windowClosing(WindowEvent e) {
    print("窗口关闭");
    System.exit(1);
  }

  @Override
  public void windowClosed(WindowEvent e) {
    print("窗口被关闭");
  }

  @Override
  public void windowIconified(WindowEvent e) {
    print("窗口最小化");
  }

  @Override
  public void windowDeiconified(WindowEvent e) {
    print("窗口从最小化恢复");
  }

  @Override
  public void windowActivated(WindowEvent e) {
    print("窗口被选中");
  }

  @Override
  public void windowDeactivated(WindowEvent e) {
    print("取消选中");
  }
}

public class JFrameTest {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Hello");

    JLabel nameLabel = new JLabel("输入用户姓名：");
    nameLabel.setBounds(10, 10, 100, 20);
    frame.add(nameLabel);

    final JTextField name = new JTextField(30);
    name.setBounds(110, 10, 120, 20);
    frame.add(name);

    JLabel noLabel = new JLabel("不可编辑：");
    noLabel.setBounds(10, 40, 100, 20);
    frame.add(noLabel);

    JTextField no = new JTextField("hans", 30);
    no.setBounds(110, 40, 120, 20);
    no.setEnabled(false);
    frame.add(no);

    JLabel passLabel = new JLabel("密码：");
    passLabel.setBounds(10, 70, 100, 20);
    frame.add(passLabel);

    final JPasswordField pass = new JPasswordField(30);
    pass.setBounds(110, 70, 120, 20);
    pass.setEchoChar('#');
    frame.add(pass);

    // 多行文本域
    JLabel testAreaLabel = new JLabel("多行文本域：");
    testAreaLabel.setBounds(10, 100, 100, 60);
    frame.add(testAreaLabel);

    final JTextArea testArea = new JTextArea("zjt_hans@126.com");
    testArea.setBounds(110, 100, 120, 60);
//    testArea.setLineWrap(true);  //内容过长换行
    frame.add(testArea);
    //滚动条
//    JScrollPane scrollPane = new JScrollPane(testArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//    frame.add(scrollPane);

    final JLabel label = new JLabel("");
    label.setBounds(0, 190, 300, 50);
    frame.add(label);

    final JButton button = new JButton("提交");
    button.setBounds(50, 170, 80, 20);
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        print(e.getSource());
        if (e.getSource() == button) {
          label.setText(name.getText() + pass.getText() + testArea.getText());
        }
      }
    });
    frame.add(button);


//    frame.setLayout(new GridLayout(4, 4));
    frame.setLayout(null);
    frame.setSize(300, 300);
    frame.setLocation(300, 200);
    frame.setVisible(true);
//    frame.addWindowListener(new MyWindowEventHandle());
    frame.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
}
